package com.kt.uokos.yimo.controller;

import com.kt.uokos.yimo.anno.validator.Order;
import com.kt.uokos.yimo.anno.validator.Sort;
import com.kt.uokos.yimo.dao.Ad;
import com.kt.uokos.yimo.service.AdService;
import com.kt.uokos.yimo.util.ResponseUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/ad")
public class AdController {

    @Resource
    private AdService adService;

    @GetMapping("/list")
    public Object list(String name, String content, @RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer limit, @Sort@RequestParam(defaultValue = "add_time")String sort, @Order@RequestParam(defaultValue = "desc")String order){
        List<Ad> adList = adService.querySelective(name, content, page, limit, sort, order);
        return ResponseUtil.okList(adList);
    }



    @PostMapping("/create")
    public Object create(@RequestBody Ad ad){
        if (ad == null ){
            return ResponseUtil.errs();
        }
        adService.add(ad);
        return ResponseUtil.ok();
    }


    @PostMapping("/update")
    public Object update(@RequestBody Ad ad){
        if (ad == null){
            return ResponseUtil.errs();
        }
        if (adService.updateById(ad) == 0){
            return ResponseUtil.errs();
        }
        return ResponseUtil.ok();
    }


    @PostMapping("delete")
    public Object delete(@RequestBody Ad ad){
        adService.delete(ad.getId());
        return ResponseUtil.ok();
    }
}
